package com.app.brilliant.sys.controller;
import com.app.brilliant.common.api.ApiCode;
import com.app.brilliant.common.api.ApiResult;
import com.app.brilliant.common.controller.BaseController;
import com.app.brilliant.common.enums.StateEnum;
import com.app.brilliant.game.service.WxUserService;
import com.app.brilliant.shiro.util.LoginUtil;
import com.app.brilliant.sys.bean.SysUser;
import com.app.brilliant.sys.params.DevUserListPageParam;
import com.app.brilliant.sys.params.WxUserListPageParam;
import com.app.brilliant.sys.service.DevUserService;
import com.app.brilliant.sys.service.SysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
@RestController
@RequestMapping("/sys")
public class SysUserController extends BaseController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private WxUserService wxUserService;
    @Autowired
    private DevUserService devUserService;

    private boolean checkUnLogin(){
        Long devId = LoginUtil.getLoginSysUserRedisVoId();
        if(devId == null){
            return true;
        }
        SysUser sysUser = sysUserService.getById(devId);
        if(sysUser == null){
            return true;
        }
        return sysUser.getState().intValue() == StateEnum.DISABLE.getCode();
    }

    @PostMapping("/appUserList")
    @ApiOperation(value = "系统客户端用户列表", notes = "系统客户端用户列表")
    public ApiResult appUserList(@RequestParam(required = false) String search, @Valid WxUserListPageParam wxUserListPageParam) {
        if(checkUnLogin()){
            return ApiResult.fail(ApiCode.UNAUTHENTICATED_EXCEPTION);
        }
        wxUserListPageParam.setKeyword(search);
        return ApiResult.ok(wxUserService.wxUserList(wxUserListPageParam));
    }

    @PostMapping("/devUserList")
    @ApiOperation(value = "任务发布者列表", notes = "任务发布者列表")
    public ApiResult devUserList(@RequestParam(required = false) String search, @Valid DevUserListPageParam devUserListPageParam) {
        if(checkUnLogin()){
            return ApiResult.fail(ApiCode.UNAUTHENTICATED_EXCEPTION);
        }
        devUserListPageParam.setKeyword(search);
        return ApiResult.ok(devUserService.devUserList(devUserListPageParam));
    }

    @PostMapping("/changeAppUserState")
    @ApiOperation(value = "修改客户端用户状态", notes = "修改客户端用户状态")
    public ApiResult changeAppUserState(Integer uid, Integer state)  {
        if(checkUnLogin()){
            return ApiResult.fail(ApiCode.UNAUTHENTICATED_EXCEPTION);
        }
        if(wxUserService.getById(uid) == null){
            return ApiResult.fail("用户不存在");
        }
        return ApiResult.ok(wxUserService.changeAppUserState(uid,state));
    }

    @PostMapping("/changeDevUserState")
    @ApiOperation(value = "修改发布者用户状态", notes = "修改发布者用户状态")
    public ApiResult changeDevUserState(Integer uid, Integer state)  {
        if(checkUnLogin()){
            return ApiResult.fail(ApiCode.UNAUTHENTICATED_EXCEPTION);
        }
        if(devUserService.getById(uid) == null){
            return ApiResult.fail("用户不存在");
        }
        return ApiResult.ok(devUserService.changeDevUserState(uid,state));
    }

    @PostMapping("/changeUserState")
    @ApiOperation(value = "修改用户状态", notes = "修改用户状态，state值只允许0或者1")
    public ApiResult<Boolean> changeUserState(@RequestParam("uid") Integer id, @RequestParam("state") Integer state) {
        if (checkUnLogin()) {
            return ApiResult.fail(ApiCode.UNAUTHENTICATED_EXCEPTION);
        }

        // 验证state值只能为0或1
        if (state != StateEnum.DISABLE.getCode() && state != StateEnum.ENABLE.getCode()) {
            return ApiResult.fail("状态值只能为0或1");
        }

        boolean result = devUserService.changeDevUserState(id, state);
        if (result) {
            return ApiResult.ok(true, "状态修改成功");
        } else {
            return ApiResult.fail("状态修改失败");
        }
    }
}
